/**
 *  Editor工具类;
 * @author    KeJun Zheng
 * @copyright (c) 2011,  WayLink
 * @version   0.1
 * @date      <ul>
 * <li>2013-08-07</li>
 * </ul>
 *
 */

var FormEditorUtil = function(){};

/**
 * render as mask chars'
 * @duplicated replaced by getFix({mask:"*",size:6})
 * @param char, size
 * @returns {Object}
 */
FormEditorUtil.getDefault = function(attr) {
	var conf={xtype: 'textfield',name:attr.name,fieldLabel:attr.text};
	Ext.applyIf(conf,attr);
	return conf;
};

FormEditorUtil.getComposited = function(attr) {
	var conf={ xtype:'compositefield',isComposite:true,isFormField:true,defaults:{xtype: 'textfield'},fieldLabel:attr.text,hideLabel:(attr.text?true:false),items:[]};
	Ext.applyIf(conf,attr);
	return conf;
};

FormEditorUtil.getCombox = function(attr) {
	var conf={xtype:'combo',typeAhead:true, triggerAction:'all', mode: 'local', fieldLabel:attr.text,lazyInit:false,lazyRender:true,
			store:new  Ext.data.Store({
				data:attr["enums"],
		        reader: new Ext.data.JsonReader({
		    		fields    : ['sid','rank','code','name','text'],
		    		root : 'list', totalProperty : 'size', idProperty:'sid'
				})}), 
				valueField:attr["valueField"] ||"code",
				displayField:attr["displayField"]||"text",
			forceSelection:true};
		if(attr.multiSelect){
			conf.xtype="bizcombo";
			conf.simpleSelect=true;
		}
	if(attr.name){
		conf.name='combo.'+attr.name;
		conf.hiddenName=attr.name;
	}
	Ext.apply(conf,attr);
	return conf;
};

FormEditorUtil.getPassword = function(attr) {
	var conf={ xtype:'compositefield',isComposite:true,isFormField:true,defaults:{xtype: 'textfield'},fieldLabel:attr.text,hideLabel:(attr.text?true:false),items:[]};
	conf.items.push({name:'map.'+attr.name});
	conf.items.push({name:'confirm.'+attr.name, title:'重复输入校验'});
	return conf;
};

/**
 * date render using given format, default is 'Y-m-d H:i:s'
 * @param fmt
 * @param conf
 * @returns Object
 */
FormEditorUtil.getDateTime = function(attr,fmt) {
	var conf={xtype:'datetimefield',format:fmt||'c',name:attr.name,fieldLabel:attr.text,value:new Date()};
	conf.altFormats="c|Y-m-d H:i:s|Y-m|H:i:s|H:i|m/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d|n-j|n/j";
	Ext.apply(conf,attr);
	return conf;
};

FormEditorUtil.getNumber = function(attr) {
	var conf={xtype:'numberfield',name:attr.name,fieldLabel:attr.text};
	if(attr.integer){conf.allowDecimals=false; }
	if(attr.positive){conf.allowNegative=false; }
	Ext.apply(conf,attr);
	return conf;
};

FormEditorUtil.getTextArea = function(attr) {
	var conf={xtype:'textarea', name:attr.name,fieldLabel:attr.text,height:160};
	Ext.apply(conf,attr);
	return conf;
};

FormEditorUtil.getHTMLEditor = function(attr) {
	var conf={xtype:'htmleditor', name:attr.name,fieldLabel:attr.text, height: 300};
	Ext.apply(conf,attr);
	return conf;
};


FormEditorUtil.getFixed = function(attr) {
	conf=FormEditorUtil.getComposited({});
	if(attr.text){
		conf.fieldLabel=attr.text;
		conf.hideLabel=false;
	}
	if(attr.prefix){
		conf.items.push({xtype: 'displayfield',value: attr.prefix});
	}
	var fld = Ext.apply({xtype:'textfield',name:attr.name},attr);
	conf.items.push(fld);
	if(attr.suffix){
		conf.items.push({xtype: 'displayfield',value: attr.suffix});
	}
	if(attr.unit){
		conf.items.push({xtype: 'displayfield',value: attr.unit});
	}
	return conf;
};

FormEditorUtil.getPicker=function(pickerUrl,e){
	var win=Ext.getCmp(pickerUrl);
	if(!win){
		win=new Ext.Window({
			frame:true,
	        plain:true,
	        id:pickerUrl,
	        width:580,
	        height:400,
	        resizable:true,
	        buttonAlign:"right",
	        maximizable:true,
	        closable:true,
	        html:"<iframe frameborder=0 src='"+pickerUrl+"' width=100% height=100%/>"
		});
	}
	win.show();
};

FormEditorUtil.getPickerField=function(pickerUrl,attr){
	var me=this,conf={
    	fieldLabel:attr["text"],
    	name:attr["name"],
    	xtype:'trigger',
    	triggerClass:'x-form-search-trigger',
    	onTriggerClick:function(e){
    		me.getPicker(pickerUrl,e);
		}
    }
	return conf;
};
/**
 * get editor by thr datatype and field attrs
 * @param dtype
 * @param fattr
 * @returns {Object}
 */
FormEditorUtil.get=function(dtype,attr1){
	var	attr = attr1||{};
	if(!attr.width){attr.width=235;}
	var conf = FormEditorUtil.getDefault(attr);
	if( attr.prefix||attr.suffix||attr.unit){
		conf=FormEditorUtil.getFixed(attr);
	}else if(attr.enums){
		conf = FormEditorUtil.getCombox(attr);
	}else if(attr.pickerUrl){
		conf=FormEditorUtil.getPickerField(attr.pickerUrl,attr);
	}else switch(dtype){
	/**
	 * java.sql.Types: BIT=-7 TINYINT=-6(8b) BIGINT=-5(64b) NULL=0 CHAR=1 NUMERIC=2 DECIMAL=3 INTEGER=4(32b) SMALLINT=5(16b) FLOAT=6(32b) REAL=7 DOUBLE=8(64b) VARCHAR=12 BOOLEAN=16
	 * DATE =91 TIME=92 TIMESTAMP=93 OTHER=1111 JAVA_OBJECT=2000 ARRAY=2003 BLOB=2004 CLOB=2005 SQLXML=2009
	 */
		case 1:
			if( attr.length>100){
				conf = FormEditorUtil.getTextArea(attr);
			}
			break;
		case 12:
				conf = FormEditorUtil.getTextArea(attr);
			break;
		case 2:
		case 3:
		case 6:
		case 7:
		case 8:
			conf = FormEditorUtil.getNumber(attr);
			break;
		case -7:
		case -6:
		case -5:
		case 0:
		case 4:
		case 5:
			conf = FormEditorUtil.getNumber(attr);
			conf.allowDecimals=false;
			break;
		case 91:
			attr.xtype='datefield';
			conf = FormEditorUtil.getDateTime(attr,'Y-m-d');
			break;
		case 92:
			attr.xtype='timefield';
			conf = FormEditorUtil.getDateTime(attr,'H:i:s');
			break;
		case 93:
			conf = FormEditorUtil.getDateTime(attr,'Y-m-d H:i:s');
			break;
		default:
	}
	return conf;
};
